<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/25 0025
 * Time: 9:19
 */
namespace app\adminapi\controller;

use app\adminapi\model\Api;
use app\api\model\JWT;
use think\App;
use think\Controller;
use think\Db;


/**
 * @title 后台雅间管理
 * @description 接口说明
 * 
 * 
 */
class Zero extends Api
{
    /**
     * @title 雅间管理
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/zero/elegant
     * @method POST
     * @param name:page type:int require:1 default: other: desc:页码
     * 
     * @return list:雅间管理@
     * @list id:雅间id name:名称 floor:区域 people:人数 addtime:添加时间
     * @return page:总页数
     */
    public function elegant()
    {
        $user = $this->user;
        $page = input('page')??1;
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['is_del','=',1];

        $list = db::name('elegant')->where($where)->order('addtime desc')->page("$page,10")->select();
        $total = db::name('elegant')->where($where)->count();
        if($list){
            foreach ($list as $k => $v) {
                $area = db::name('area')->where('id',$v['qid'])->field('id,name')->find();
                if($area){
                    $list[$k]['floor'] = urldecode($area['name']);
                }
                $areas = db::name('area')->where('id',$v['pid'])->field('id,name')->find();
                if($areas){
                    $list[$k]['people'] = urldecode($areas['name']);
                }
                $list[$k]['addtime'] = date('Y-m-d H:i:s',$list[$k]['addtime']);
            }
        }
        $data['list'] = $list;
        $data['page'] = ceil($total/10);
        $data['pages'] = 10;
        $data['total'] = $total;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 获取区域和人数
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/zero/elegant_add
     * @method POST
     * @param name:type type:int require:1 default: other: desc:1获取区域2获取人数
     * 
     * @return list:列表@
     * @list id:id name:名称
     */
    public function elegant_add()
    {
        $user = $this->user;
        $type = input('type');
        if(!input('type')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['type','=',$type];
        $where[] = ['is_hidden','=',1];
        $list = db::name('area')->where($where)->order('addtime desc')->select();
        $data['list'] = $list;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 雅间保存和修改
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/zero/elegant_save
     * @method POST
     * @param name:name type:int require:1 default: other: desc:名称
     * @param name:id type:int require:0 default: other: desc:需修改id
     * @param name:qid type:int require:1 default: other: desc:所选区域id
     * @param name:pid type:int require:1 default: other: desc:人数id
     * @param name:type type:int require:1 default: other: desc:1添加2修改
     * 
     */
    public function elegant_save()
    {
        $user = $this->user;
        if(!input('qid')){
            return $this->ajaxReturn(1,'请选择区域');
        }
        if(!input('pid')){
            return $this->ajaxReturn(1,'请选择人数');
        }
        if(!input('name')){
            return $this->ajaxReturn(1,'请输入名称');
        }
        $type = input('type');
        $id = input('id');

        $up['name'] = input('name');
        $up['qid'] = input('qid');
        $up['pid'] = input('pid');
        if($type == 2){
            db::name('elegant')->where('id',$id)->update($up);
        }elseif($type == 1){
            $up['addtime'] = time();
            $up['is_hidden'] = 1;
            $up['hid'] = $user['hid'];
            db::name('elegant')->insert($up);
        }
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 雅间修改获取数据
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/zero/elegant_edit
     * @method POST
     * @param name:id type:int require:1 default: other: desc:雅间id
     * 
     * @return list:列表@
     * @list id:id name:名称 pid:区域id qid:人数id
     */
    public function elegant_edit()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $elegant = db::name('elegant')->where('id',$id)->field('id,pid,qid,name')->find();
        $data['list'] = $elegant;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 雅间删除
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/zero/elegant_del
     * @method POST
     * @param name:id type:int require:1 default: other: desc:雅间id
     * 
     */
    public function elegant_del()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        // $time = date('Y-m-d',time());
        // $where[] = ['eid','=',$id];
        // $where[] = ['hid','=',$user['hid']];

        // $str = explode("-",$time);
        // $where[] = ['year','=',$str[0]];
        // $where[] = ['month','>=',$str[1]];
        // $where[] = ['day','>=',$str[2]];
        // $where[] = ['status','=',1];

        // $order = db::name('zero')->where($where)->select();
        // if($order){
        //     return $this->ajaxReturn(1,'此雅间有未入座的订单不可删除');
        // }
        db::name('elegant')->where('id',$id)->update(['is_del'=>2]);
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 人数与餐区
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/zero/people
     * @method POST
     * @param name:type type:int require:1 default: other: desc:1餐区2人数
     * @param name:page type:int require:1 default: other: desc:页码
     * 
     * @return list:列表@
     * @list id:id name:名称 is_hidden:1显示2不显示 type:类型1餐区2人数
     * @return page:总页数
     */
    public function people()
    {
        $user = $this->user;
        if(!input('type')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $page = input('page')??1;
        $type = input('type');
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['type','=',$type];
        $where[] = ['is_del','=',1];
        $people = db::name('area')->where($where)->field('id,name,is_hidden,type,addtime')->page("$page,10")->select();
        if($people){
            foreach ($people as $k => $v) {
                $people[$k]['addtime'] = date('Y-m-d H:i:s',$people[$k]['addtime']);
            }
        }
        $total = db::name('area')->where($where)->count();
        $data['list'] = $people;
        $data['page'] = ceil($total/10);
        $data['pages'] = 10;
        $data['total'] = $total;
        return $this->ajaxReturn(0,'成功',$data);
    }
     /**
     * @title 人数与餐区的修改获取信息
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/zero/people_edit
     * @method POST
     * @param name:id type:int require:1 default: other: desc:id
     * 
     * @return list:列表@
     * @list id:id name:名称  type:类型1餐区2人数 is_hidden:1显示2隐藏
     */
    public function people_edit()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $area = db::name('area')->where('id',$id)->field('id,name,type,is_hidden')->find();
        $data['list'] = $area;
        return $this->ajaxReturn(0,'成功',$data);
    }

    /**
     * @title 人数与餐区的修改新增
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/zero/people_add
     * @method POST
     * @param name:id type:int require:0 default: other: desc:修改id
     * @param name:name type:int require:1 default: other: desc:名称
     * @param name:type type:int require:1 default: other: desc:类型1餐区2人数
     * @param name:is_hidden type:int require:1 default: other: desc:1显示2隐藏
     * @param name:is_type type:int require:1 default: other: desc:1新增2修改
     * 
     */
    public function people_add()
    {
        $user = $this->user;
        if(!input('is_type')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        if(!input('name')){
            return $this->ajaxReturn(1,'请输入名称');
        }
        if(!input('is_hidden')){
            return $this->ajaxReturn(1,'请选择启用禁用');
        }
        $type = input('type');
        $id = input('id');
        $name = input('name');
        $is_hidden = input('is_hidden');
        $is_type = input('is_type');

        $up['name'] = $name;
        $up['is_hidden'] = $is_hidden;
        if($is_type == 1){
            $up['type'] = $type;
            $up['addtime'] = time();
            $up['hid'] = $user['hid'];
            db::name('area')->insert($up);
        }elseif($is_type == 2){
            db::name('area')->where('id',$id)->update($up);
        }
        return $this->ajaxReturn(0,'成功');
    }
     /**
     * @title 人数与餐区的启用禁用
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/zero/people_change
     * @method POST
     * @param name:id type:int require:1 default: other: desc:id
     * @param name:type type:int require:1 default: other: desc:类型1餐区2人数
     */
    public function people_change()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $type = input('type');
        $status = db::name('area')->where('id',$id)->value('is_hidden');

        $time = date('Y-m-d',time());
        $where[] = ['hid','=',$user['hid']];
        $str = explode("-",$time);
        $where[] = ['year','=',$str[0]];
        $where[] = ['month','>=',$str[1]];
        $where[] = ['day','>=',$str[2]];
        $where[] = ['status','=',1];
        if($type == 1){
            $where[] = ['qid','=',$id];
            $order = db::name('zero')->where($where)->select();
        }elseif($type == 2){
            $where[] = ['pid','=',$id];
            $order = db::name('zero')->where($where)->select();
        }
        // if($order){
        //     return $this->ajaxReturn(1,'有未完成订单,暂不可操作'); 
        // }
        if($status == 1){
            $up['is_hidden'] = 2;
            db::name('area')->where('id',$id)->update($up);
        }elseif($status == 2){
            $up['is_hidden'] = 1;
            db::name('area')->where('id',$id)->update($up);
        }
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 人数与餐区的删除
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/zero/people_del
     * @method POST
     * @param name:id type:int require:1 default: other: desc:id
     * @param name:type type:int require:1 default: other: desc:类型1餐区2人数
     */
    public function people_del()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        // $type = input('type');
        // $time = date('Y-m-d',time());
        // $where[] = ['eid','=',$id];
        // $where[] = ['hid','=',$user['hid']];

        // $str = explode("-",$time);
        // $where[] = ['year','=',$str[0]];
        // $where[] = ['month','>=',$str[1]];
        // $where[] = ['day','>=',$str[2]];
        // $where[] = ['status','=',1];

        // $order = db::name('zero')->where($where)->select();
        // if($order){
        //     return $this->ajaxReturn(1,'此配置下的雅间有未入座的订单不可删除');
        // }
        db::name('area')->where('id',$id)->update(['is_del'=>2]);
        return $this->ajaxReturn(0,'成功');
    }
}